package com.mazaiting.classfile.attr.info;

import com.mazaiting.classfile.ClassReader;
import com.mazaiting.classfile.attr.entity.ClassesEntity;
import com.mazaiting.classfile.constant.ConstantPool;

import java.util.Arrays;

/**
 * 内部类属性
 *
 * @author mazaiting
 * @since 2021/8/13
 */
public class InnerClassesAttribute extends AttributeInfo{
    /**
     * 类数量 无符号 u2
     */
    private int numberOfClasses;
    /**
     * 类实体数组
     */
    private ClassesEntity[] classesEntities;

    public InnerClassesAttribute(ConstantPool pool, int index, int length) {
        super(pool, index, length);
    }

    @Override
    public void readInfo(ClassReader reader) {
        this.numberOfClasses = reader.readU2ToUnsignedShort();
        this.classesEntities = new ClassesEntity[numberOfClasses];
        for (int i = 0; i < numberOfClasses; i++) {
            this.classesEntities[i] = new ClassesEntity(
                    reader.readU2ToUnsignedShort(),
                    reader.readU2ToUnsignedShort(),
                    reader.readU2ToUnsignedShort(),
                    reader.readU2ToUnsignedShort()
            );
        }
    }

    @Override
    public String toString() {
        return "InnerClassesAttribute{" +
                "nameIndex=" + nameIndex +
                ", length=" + length +
                ", numberOfClasses=" + numberOfClasses +
                ", classesEntities=" + Arrays.toString(classesEntities) +
                '}';
    }
}


















